package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	/**
	 * 用户首次注册成功后，为用户初始账户余额记录
	 *
	 * @param map 消息对象 包含：用户ID，赠送体验金额，标题，订单编号
	 */
	@Override
	public void initUserAccount(Map<String, Object> map) {

		Long userId = (Long) map.get("userId");
		BigDecimal amount = (BigDecimal) map.get("amount");
		String title = (String) map.get("title");
		String orderNo = (String) map.get("orderNo");
		//新增用户账户表信息
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(amount);
		userAccount.setAvailableAmount(amount);
		userAccount.setLockAmount(BigDecimal.ZERO);
		userAccount.setTotalIncomeAmount(amount);
		userAccount.setTotalPayAmount(BigDecimal.ZERO);
		userAccountMapper.insert(userAccount);

		//新增用户账户明细表信息
		this.saveUserAccountDetail(userId,title, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT,amount,orderNo);
	}

	@Override
	public void saveUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);

		userAccountDetailMapper.insert(userAccountDetail);
   	}


	/**
	 * 获取账户可用余额
	 * @return
	 */
	@Override
	public BigDecimal getAvailableAmount(Long userId) {

		UserAccount userAccount = userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccount>()
				.eq(UserAccount::getUserId, userId)
		);

		if (userAccount != null){
			return userAccount.getAvailableAmount();
		}

		return new BigDecimal("0.00");
	}


	/**
	 * 检查余额及扣减余额
	 * @param accountDeductVo
	 * @return
	 */
	@Override
	public void checkAndDeduct(AccountDeductVo accountDeductVo, Long userId) {
		String orderNo = accountDeductVo.getOrderNo();

		BigDecimal amount = accountDeductVo.getAmount();

		String content = accountDeductVo.getContent();

		//首先检查余额和扣减金额的信息,是否满足扣款条件
		//获余额
		UserAccount userAccount = userAccountMapper.checkDeduction(userId,amount);

		if (userAccount == null){
			throw new GuiguException(500,"账户余额不足");
		}

		//走到这说明账户余额足够可以执行正常业务了
		//账户表的总金额要减少,可用金额要减少,总支出要增多,其他不变

		//扣减金额,更新用户金额表
		userAccountMapper.update(null,new LambdaUpdateWrapper<UserAccount>()
				.eq(UserAccount::getUserId,userId)
				.setSql("total_amount = total_amount - " + amount)
				.setSql("available_amount = available_amount -" + amount)
				.setSql("total_pay_amount = total_pay_amount -" + amount)
		);

		//更新扣减金额明细表
		UserAccountDetail userAccountDetail = new UserAccountDetail();

		userAccountDetail.setUserId(userId);

		userAccountDetail.setTitle(content);

		userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);

		userAccountDetail.setAmount(amount);

		userAccountDetail.setOrderNo(orderNo);

		userAccountDetailMapper.insert(userAccountDetail);

	}
}
